사이트 내 전체검색
PHP
[MS-SQL] 열이 집계 함수나 GROUP BY 절에 없으므로 SELECT 목록에서 사용할 수 없습니다.
로빈아빠
https://cmd.kr/php/731 URL이 복사되었습니다.

본문

<수정전>
select (
                (case substring(jumin,8,1)
                          when '1' then year(getdate()) - (left(jumin,2)+1900)
                          when '2' then year(getdate()) - (left(jumin,2)+1900)
                          else  year(getdate()) - (left(jumin,2)+1900)
                   end )
                       /10) *10 'age', count(*) 'age_total', c1idx 'c1idx', hope1 'hope1'
from fpr_sin100
group by ((case substring(jumin,8,1)
  when '1' then year(getdate()) - (left(jumin,2)+1900)
  when '2' then year(getdate()) - (left(jumin,2)+1900)
  else  year(getdate()) - (left(jumin,2)+1900)
  end )/10) *10, c1idx 

<오류메시지>
메시지 8120, 수준 16, 상태 1, 줄 1
'fpr_sin100.hope1' 열이 집계 함수나 GROUP BY 절에 없으므로 SELECT 목록에서 사용할 수 없습니다.



[ 답변 ]
sum()이나 count(), max()등은 집계함수라고 하며, group by와 함께 사용하는 경우는 집계함수에 쌓이지 않은 컬럼들은 전부 group by list에 있어야 합니다.

또한 group by를 안쓰려는 경우는 집계함수가 하나도 없던지, 모든 컬럼들이 전부 집계함수로 들어가 있어야 합니다.

<수정>
select (
                (case substring(jumin,8,1)
                          when '1' then year(getdate()) - (left(jumin,2)+1900)
                          when '2' then year(getdate()) - (left(jumin,2)+1900)
                          else  year(getdate()) - (left(jumin,2)+1900)
                   end )
                       /10) *10 'age', count(*) 'age_total', c1idx 'c1idx', hope1 'hope1'
from fpr_sin100
group by ((case substring(jumin,8,1)
  when '1' then year(getdate()) - (left(jumin,2)+1900)
  when '2' then year(getdate()) - (left(jumin,2)+1900)
  else  year(getdate()) - (left(jumin,2)+1900)
  end )/10) *10, c1idx, hope1   

댓글목록

등록된 댓글이 없습니다.

PHP
871 (1/18P)

Search

Copyright © Cmd 명령어 18.224.31.121